﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Events.aspx.cs" Inherits="DonationTracker.Events" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" >
        function text_change(t) {
            var val = t.value;
            var l = val.length;
            if (val == "") { l = 0; }
            var btn = document.getElementById('MainContent_GridView1_btnSave');
            if (l > 0) {
                btn.disabled = false;
            } else {
                btn.disabled = true;
            }
        }

        window.onload = function () {
            var d = document.getElementById('gridContainer');
            var g = document.getElementById('MainContent_GridView1');
            d.style.width = g.offsetWidth + "px";
            g.align = "center";
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Events Page</h2>
<br /><br />
<asp:SqlDataSource ID="getCharities" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [name], [charityID] FROM [charity]"></asp:SqlDataSource>
<asp:SqlDataSource ID="donationTracker_dataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [eventID], event.name, event.description, charity.name as charityID from event join charity on event.charityID = charity.charityID" ConflictDetection="CompareAllValues" 
        DeleteCommand="DELETE FROM [event] WHERE [eventID] = @original_eventID AND [name] = @original_name AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL)) AND [charityID] = @original_charityID" 
        InsertCommand="INSERT INTO [event] ([name], [description], [charityID]) VALUES (@name, @description, @charityID)" 
        OldValuesParameterFormatString="original_{0}" 
        UpdateCommand="UPDATE [event] SET [name] = @name, [description] = @description, [charityID] = @charityID WHERE [eventID] = @original_eventID">
    <DeleteParameters>
        <asp:Parameter Name="original_eventID" Type="Int32" />
        <asp:Parameter Name="original_name" Type="String" />
        <asp:Parameter Name="original_description" Type="String" />
        <asp:Parameter Name="original_charityID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="description" Type="String" />
        <asp:Parameter Name="charityID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="description" Type="String" />
        <asp:Parameter Name="charityID" Type="Int32" />
        <asp:Parameter Name="original_eventID" Type="Int32" />
    </UpdateParameters>
    </asp:SqlDataSource>
    <div id="gridContainer" style="width: 800px; max-width: 960px; margin: 0px auto;" >
        <asp:GridView ID="GridView1" runat="server" 
            style="max-height: 400px; width: 800px; margin: 0px auto;" AllowPaging="True" 
            AutoGenerateColumns="False" DataKeyNames="eventID" 
            DataSourceID="donationTracker_dataSource" 
            onrowcommand="GridView1_RowCommand" onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="eventID" InsertVisible="False" 
                    SortExpression="eventID">
                    <FooterTemplate>
                        <asp:Button ID="btnSave" runat="server" CommandName="Insert" Enabled="False" 
                            Text="Save" />
                        <asp:Button ID="btnCancel" runat="server" CausesValidation="False" 
                            onclick="btnCancel_Click" Text="Cancel" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("eventID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name" SortExpression="name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewName" runat="server" MaxLength="50" onkeyup="text_change(this)" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvNewName" runat="server" 
                            ControlToValidate="txtNewName" Display="Dynamic" 
                            ErrorMessage="Please enter an event name." ForeColor="Red"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="description" SortExpression="description">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditDesc" runat="server" Text='<%# Bind("description") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewDesc" runat="server" MaxLength="150"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("description") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="charityID" SortExpression="charityID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEditCharityID" runat="server" DataSourceID="getCharities" 
                            DataTextField="name" DataValueField="charityID">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlNewCharityID" runat="server" DataSourceID="getCharities" 
                            DataTextField="name" DataValueField="charityID">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("charityID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnAddEvent" runat="server" Text="Add Event" 
            onclick="btnAddEvent_Click" />
    </div>
</asp:Content>